<!-- 歌曲队列组件 -->
<template>
  <transition name="list-fade">
    <!-- 点击关闭歌曲列队组件 -->
    <div class="playlist" v-show="showFlag" @click="hide">
      <!-- 整个的弹出的内容层 -->
      <!-- 禁止冒泡 -->
      <div class="list-wrapper" @click.stop>
        <!-- 头部 -->
        <div class="list-header">
          <h1 class="title">
            <!-- 左上侧播放模式标识 -->
            <!-- 根据mode来切换样式 -->
            <!-- 点击切换模式 -->
            <i class="icon" :class="iconMode" @click="changeMode"></i>
            <span class="text">{{modeText}}</span>
            <!-- 点击是否清除歌曲列表 -->
            <span class="clear" @click="showConfrim"><i class="icon-clear"></i></span>
          </h1>
        </div>
        <!-- 中间列表 -->
        <scroll ref="listContent" :refreshDelay="refreshDelay" :data="sequenceList" class="list-content">
          <!-- vue中提供的标签transition-group和transition一样,但是transition-group元素的子元素需要一个key -->
          <transition-group name="list" tag="ul">
            <!-- 点击对应的歌曲播放 -->
            <li ref="listItem" class="item" @click="selectItem(item,index)" v-for="(item,index) in sequenceList" :key="item.id">
              <i class="current" :class="getCurrentIcon(item)"></i>
              <span class="text">{{item.name}}</span>
              <!-- 点击收藏 -->
              <span @click.stop="toggleFavorite(item)" class="like">
                <i :class="getFavoriteIcon(item)"></i>
              </span>
              <!-- 在列表中点击删除歌曲 -->
              <span class="delete" @click.stop="deleteOne(item)">
                <i class="icon-delete"></i>
              </span>
            </li>
          </transition-group>
        </scroll>
        <!-- 添加歌曲到队列 -->
        <div class="list-operate">
          <div class="add" @click="addSong">
            <i class="icon-add"></i>
            <span class="text">添加歌曲到队列</span>
          </div>
        </div>
        <!-- 点击关闭歌曲列队组件 -->
        <div @click="hide" class="list-close">
          <span>关闭</span>
        </div>
      </div>
      <!-- 点击清除歌曲的弹出框 -->
      <confirm ref="confrim" @confirm="confrimClear" text="是否清空播放列表" confirmBtnText="清空"></confirm>
      <!-- 添加歌曲到列表层 -->
      <add-song ref="addSong"></add-song>
    </div>
  </transition>
</template>

<script>
// vuex
import {mapActions} from 'vuex'
// 播放顺序的值
import {playMode} from 'common/js/config'
// scroll组件
import Scroll from 'base/scroll/scroll'
// Confirm组件是点击清除歌曲的弹出框
import Confirm from 'base/confirm/confirm'
// mixin
import {playerMixin} from 'common/js/mixin'
// add-song组件
import AddSong from 'components/add-song/add-song'

export default {
  mixins: [playerMixin],
  data () {
    return {
      showFlag: false,
      refreshDelay: 100
    }
  },
  computed: {
    modeText () {
      return this.mode === playMode.sequence ? '顺序播放' : this.mode === playMode.random ? '随机播放' : '单曲循环'
    }
  },
  methods: {
    show () {
      this.showFlag = true
      setTimeout(() => {
        this.$refs.listContent.refresh()
        // 刚显示的时候组件滚动到currentSong
        this.scrollToCurrent(this.currentSong)
      }, 20)
    },
    hide () {
      this.showFlag = false
    },
    // 给对应的元素添加及时的class
    getCurrentIcon (item) {
      if (this.currentSong.id === item.id) {
        return 'icon-play'
      }
      return ''
    },
    // 点击改变相应的歌曲播放
    selectItem (item, index) {
      // 如果播放模式是随机播放
      if (this.mode === playMode.random) {
        // 找到在数组中对应的位置
        index = this.playlist.findIndex((song) => {
          return song.id === item.id
        })
      }
      // 播放当前点击的元素
      this.setCurrentIndex(index)
      // 播放状态为播放
      this.setPlayingState(true)
    },
    // 滚动到当前播放歌曲位置
    scrollToCurrent (current) {
      // 在播放列表中找到歌曲在数组中的位置
      const index = this.sequenceList.findIndex((song) => {
        // 用current的id和数组每项中id相等来判定
        return current.id === song.id
      })
      // 调用scroll组件的方法,滚动到listItem这个数组的当前歌曲的这个位置
      this.$refs.listContent.scrollToElement(this.$refs.listItem[index], 300)
    },
    // 在列表中点击删除歌曲
    deleteOne (item) {
      this.deleteSong(item)
      // 如果在歌曲列表没有歌曲的时候
      if (!this.playlist.length) {
        // 关闭歌曲列队组件
        this.hide()
      }
    },
    // 清空列表
    confrimClear () {
      this.deleteSongList()
      // 关闭歌曲列队组件
      this.hide()
    },
    // 点击显示组件
    showConfrim () {
      this.$refs.confrim.show()
    },
    // 点击添加歌曲到列表
    addSong () {
      // 打开addsong组件
      this.$refs.addSong.show()
    },
    ...mapActions([
      // 在列表中删除歌曲
      'deleteSong',
      // 清空歌曲列表
      'deleteSongList'
    ])
  },
  watch: {
    // 播放的具体哪一项
    currentSong (newSong, oldSong) {
      // 如果组件不显示
      if (!this.showFlag || newSong.id === oldSong) {
        return
      }
      // 否则滚动到这里
      this.scrollToCurrent(newSong)
    }
  },
  components: {
    Scroll,
    Confirm,
    AddSong
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"
  @import "~common/stylus/mixin"

  .playlist
    position: fixed
    left: 0
    right: 0
    top: 0
    bottom: 0
    z-index: 200
    background-color: $color-background-d
    &.list-fade-enter-active, &.list-fade-leave-active
      transition: opacity 0.3s
      .list-wrapper
        transition: all 0.3s
    &.list-fade-enter, &.list-fade-leave-to
      opacity: 0
      .list-wrapper
        transform: translate3d(0, 100%, 0)
    &.list-fade-enter
    .list-wrapper
      position: absolute
      left: 0
      bottom: 0
      width: 100%
      background-color: $color-highlight-background
      .list-header
        position: relative
        padding: 20px 30px 10px 20px
        .title
          display: flex
          align-items: center
          .icon
            margin-right: 10px
            font-size: 30px
            color: $color-theme-d
          .text
            flex: 1
            font-size: $font-size-medium
            color: $color-text-l
          .clear
            extend-click()
            .icon-clear
              font-size: $font-size-medium
              color: $color-text-d
      .list-content
        max-height: 240px
        overflow: hidden
        .item
          display: flex
          align-items: center
          height: 40px
          padding: 0 30px 0 20px
          overflow: hidden
          &.list-enter-active, &.list-leave-active
            transition: all 0.1s
          &.list-enter, &.list-leave-to
            height: 0
          .current
            flex: 0 0 20px
            width: 20px
            font-size: $font-size-small
            color: $color-theme-d
          .text
            flex: 1
            no-wrap()
            font-size: $font-size-medium
            color: $color-text-d
          .like
            extend-click()
            margin-right: 15px
            font-size: $font-size-small
            color: $color-theme
            .icon-favorite
              color: $color-sub-theme
          .delete
            extend-click()
            font-size: $font-size-small
            color: $color-theme
      .list-operate
        width: 140px
        margin: 20px auto 30px auto
        .add
          display: flex
          align-items: center
          padding: 8px 16px
          border: 1px solid $color-text-l
          border-radius: 100px
          color: $color-text-l
          .icon-add
            margin-right: 5px
            font-size: $font-size-small-s
          .text
            font-size: $font-size-small
      .list-close
        text-align: center
        line-height: 50px
        background: $color-background
        font-size: $font-size-medium-x
        color: $color-text-l
</style>
